package active

import (
	"server/game/cache"
	"server/share/config"

	"github.com/hwcer/cosgo"
	"github.com/hwcer/cosgo/times"
	"github.com/hwcer/updater"
	"github.com/hwcer/yyds/errors"
	"github.com/hwcer/yyds/players"
	"github.com/hwcer/yyds/players/player"
)

func init() {
	cosgo.On(cosgo.EventTypLoaded, func() error {
		i := BattlePassModel{}
		players.On(config.Data.Events.OrderVerify, i.verify)
		players.On(config.Data.Events.OrderSubmit, i.Submit)
		return nil
	})
}

// 战令
func NewBattlePassModel() *BattlePassModel {
	return &BattlePassModel{Task: map[int32]int64{}}
}

type BattlePassWeek struct {
	Score  int32 `json:"score" bson:"score"`   //本周积分
	Expire int64 `json:"expire" bson:"expire"` //过期时间。本周的结束时间点
}
type BattlePassModel struct {
	Lv    int32           `json:"lv" bson:"lv"`       //战令等级
	Exp   int32           `json:"exp" bson:"exp"`     //战令经验
	Free  int32           `json:"free" bson:"free"`   //免费奖励
	Rare  int32           `json:"rare" bson:"rare"`   //高级奖励
	Grade int32           `json:"grade" bson:"grade"` //战令等级，battlePassGrade.id
	Task  map[int32]int64 `json:"task" bson:"task"`   //任务进度，id --> 过期时间，0-不过期，赛季任务
	Week  *BattlePassWeek `json:"week" bson:"week"`
}

func (this *BattlePassModel) GetWeek() *BattlePassWeek {
	r := this.Week

	if r == nil || times.Now().Unix() > r.Expire {
		r = &BattlePassWeek{}
		wt, _ := times.Expire(times.ExpireTypeWeekly, 1)
		r.Expire = wt.Now().Unix()
	}

	return r

}

func (this *BattlePassModel) verify(u *updater.Updater, args ...int32) {
	if len(args) == 0 {
		return
	}
	iid := args[0]
	c := config.Data.Order[iid]
	if c == nil {
		return
	}
	if len(c.Caller) < 3 || c.Caller[0] != config.Data.Caller.BattlePass {
		return
	}
	//活动验证
	ac := config.Data.Active[c.Caller[1]]
	if ac == nil {
		_ = u.Errorf("config order.Caller error:%d", iid)
		return
	}
	if ac.Mod != config.Data.ActiveMod.BattlePass {
		_ = u.Errorf("active config mod mismatched")
		return
	}
	//战令设置
	rc := config.Data.BattlePass[ac.Args[0]]
	if rc == nil {
		u.Error = errors.ErrConfigEmpty
		return
	}
	handle := cache.GetActive(u)
	item, _, err := handle.Verify(ac.Id)
	if err != nil {
		u.Error = err
		return
	}

	//购买战令配置
	battlePassGrade := config.Data.BattlePassGrade[c.Caller[2]]
	if battlePassGrade == nil {
		_ = u.Errorf("battlePassPay not exist:%d", c.Caller[1])
		return
	}
	if battlePassGrade.Group != rc.Id {
		_ = u.Errorf("battlePassPay group mismatched")
		return
	}
	d := &BattlePassModel{}
	if u.Error = item.Unmarshal(d); u.Error != nil {
		return
	}
	if d.Grade >= battlePassGrade.Id {
		_ = u.Errorf("active battlePass pay exist")
		return
	}

}

// Submit 发放战令
// 参数args [活动ID，]
func (this *BattlePassModel) Submit(u *updater.Updater, args ...int32) {
	if len(args) == 0 {
		return
	}
	iid := args[0]
	c := config.Data.Order[iid]
	if c == nil {
		u.Error = errors.ErrConfigEmpty
		return
	}
	if len(c.Caller) < 3 || c.Caller[0] != config.Data.Caller.BattlePass {
		//_ = u.Errorf("config order.Caller error:%d", iid)
		return
	}
	//活动验证
	ac := config.Data.Active[c.Caller[1]]
	if ac == nil {
		_ = u.Errorf("config order.Caller error:%d", iid)
		return
	}
	if ac.Mod != config.Data.ActiveMod.BattlePass {
		_ = u.Errorf("active config mod mismatched")
		return
	}
	//战令设置
	rc := config.Data.BattlePass[ac.Args[0]]
	if rc == nil {
		u.Error = errors.ErrConfigEmpty
		return
	}
	handle := cache.GetActive(u)
	item, _, err := handle.Verify(ac.Id)
	if err != nil {
		u.Error = err
		return
	}
	d := &BattlePassModel{}
	if u.Error = item.Unmarshal(d); u.Error != nil {
		return
	}
	//购买战令配置
	battlePassGrade := config.Data.BattlePassGrade[c.Caller[2]]
	if battlePassGrade == nil {
		_ = u.Errorf("battlePassPay not exist:%d", c.Caller[1])
		return
	}
	if battlePassGrade.Group != rc.Id {
		_ = u.Errorf("battlePassPay group mismatched")
		return
	}
	if d.Grade == 0 {
		d.Grade = battlePassGrade.Group*100 + 0
	}
	p := u.Player().(*player.Player)
	//奖励
	for i := d.Grade + 1; i <= battlePassGrade.Id; i++ {
		if bc := config.Data.BattlePassGrade[i]; bc != nil {
			d.Lv += bc.Level
			p.AddItems(bc.Items)
		}
	}

	d.Grade = battlePassGrade.Id
	handle.SetAttach(item.OID, d)
}
